// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../../../../script/game/control/game";
import { figureBase } from "../../../../script/game/control/Item/ItemBase";
import { diseaseType, hospitalType } from "../../../../script/game/gameType";
import figureModel from "../../../../script/game/model/figureModel";
import { ListViewAdapter } from "../../../../script/util/ListView/ListViewAdapter";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";
import illnessItem from "../../active/items/illnessItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    @property(ListViewAdapter)
    listView: ListViewAdapter = null;

    @property(cc.Node)
    tips: cc.Node = null;
    
    percent: number = 0.8;
    hospital: hospitalType = null;
    list: diseaseType[] = [];
    hero: figureModel = null;
    start () {
        this.registerEvent('reDisease', () => {
            this.getData();
        })
        this.getData();
    }
    getData() {
        this.hospital = ccs.dataMgr.getHospitalById(this._args.id);
        if(this.hospital) {
            //给自己治病的
            this.percent = this.hospital.money;
            this.list = game.playerMgr.heroInfo.disease;
            this.list.filter(val => val.level <= this.hospital.level);
            if(this.list.length > 0) {
                this.listView.updateItems(this.list);
            } else {
                this.tips.active = true;
            }
        } else {
            let data = game.playerMgr.bag.figureBag.getItem(this._args.heroId);
            this.hero = new figureModel(data.data.relation, data.data.hero);
            this.list = this.hero.heroInfo.disease;
            if(this.list.length > 0) {
                this.list.sort((a, b) => a.level - b.level);
                this.hospital = ccs.dataMgr.getHospitalByLevel(this.list[0].level);
                this.percent = this.hospital.money;
                this.listView.updateItems(this.list);
            } else {
                this.listView.node.active = false;
                this.tips.active = true;
            }
        }
    }
    onCell(node: cc.Node, data: diseaseType, idx: number) {
        node.getComponent(illnessItem).setData(data, this.percent, this.hospital.percent);
        this.touchEndEventScale(node, () => {
            if(this.hero) {
                this.hero.goHospital(data);
            } else {
                game.playerMgr.goHospital(data, false, this.hospital);
            }
            this._close();
        })
    }
    // update (dt) {}
}
